package savehalloween;

import java.io.IOException;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

import savehalloween.monster.PumpkinZombie;
import savehalloween.tower.PumpkinSeedGun;

/**
 *
 * @author wutzelputz
 */
public class PlayCanvas extends GameCanvas implements Runnable{

    private Monster myPump;
    private Sprite sweets;
    private Grid grid;
    private Tower myTower;
    

    public PlayCanvas(){
        super(false);
    }

    public void start(){
        // create a pumpkin        
        myPump = new PumpkinZombie().setX(100).setY(0);
        grid = new Grid();
        myTower = new PumpkinSeedGun();
        grid.addTowerAt(myTower, 2, 2);
        try {
			Image sweetsImage = Image.createImage("sweets.png");
			sweets = new Sprite(sweetsImage, sweetsImage.getWidth(), sweetsImage.getHeight());
	        sweets.defineReferencePixel((int)sweetsImage.getWidth()/2, (int) sweetsImage.getHeight()/2);
	        sweets.setRefPixelPosition(120, 260);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        Thread runner = new Thread(this);
        
        runner.run();
    }

    public void run(){
        Graphics g = getGraphics();

        while(true){
            paint(g);
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    protected void pointerPressed(int x, int y) {
    	System.out.println(x + "/" + y + " pressedeleased");
      }
    
    protected void pointerReleased(int x, int y) {
        System.out.println(x + "/" + y + " released");
    }

    public void paint(Graphics g){
    	drawBackground(g);
        grid.paint(g);
        sweets.paint(g);
        myPump.paint(g);
        flushGraphics();
    }
    
    private void drawBackground(Graphics g){
    	g.setColor(0x8AE234);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}
